#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define PORT 1234
#define BACKLOG 5
#define MAXDATASIZE 1000


void process_cli(int connfd, struct sockaddr_in client);

int main(){
    int listenfd, connfd;
    pid_t pid;
    struct sockaddr_in server, client;

    // 产生该进程使用的套接字
    if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
        perror("Creating socket failed.\n");
        exit(1);
    }

    // 设置套接字选项
    int opt = SO_REUSEADDR;
    setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
    
    // 设置该服务器的套接字的地址以及端口
    bzero(&server, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(PORT);
    server.sin_addr.s_addr = htonl(INADDR_ANY);

    // 将服务器套接字与地址结构绑定
    if(bind(listenfd, (struct sockaddr*) &server, sizeof(server)) == -1){
        perror("bind error.\n");
        exit(1);
    }
    
    // 将该套接字转换为监听套接字
    if(listen(listenfd, BACKLOG) == -1){
        perror("listen error.\n");
        exit(1);
    }

    socklen_t len = sizeof(client);
    while(1){
        // 等待客户端发送请求,返回已连接套接字和客户端的地址
        if((connfd = accept(listenfd, (struct sockaddr*) &client, &len)) == -1){
            perror("accept error.\n");
            exit(1);
        }

        // 连接成功，产生子进程
        if((pid = fork()) > 0){
            /*父进程
             *父进程关闭已连接描述符，继续接收下一个客户的连接请求
             * 父进程不需要继续与客户端连接，需要监听新的客户请求 
             */
            close(connfd);
            continue;
        }else if(pid == 0){
            /*子进程
             * 子进程关闭监听监听套接字描述符,然后进行请求的处理,子进程通过exit返回
             * 子进程不需要监听新的客户请求,只需要保持与客户端的连接
             */
            close(listenfd);
            process_cli(connfd, client);
            exit(0);
        }else{
            printf("fork error.\n");
            exit(0);
        }
    }

    close(listenfd);

    return 0;
       
}

void process_cli(int connfd, struct sockaddr_in client){
    int num;
    char recvbuf[MAXDATASIZE], sendbuf[MAXDATASIZE], cli_name[MAXDATASIZE];
    printf("You got a connection from %s.\n", inet_ntoa(client.sin_addr));

    // 接收客户端发来的名字
    if(num = recv(connfd, cli_name, MAXDATASIZE, 0) == 0){
        close(connfd);
        printf("Client disconnected.\n");
        return;
    }

    cli_name[num - 1] = '\0';
    printf("Client's name is %s.\n", cli_name);
    // 接收客户端发送的数据
    while(num = recv(connfd, recvbuf, MAXDATASIZE, 0)){
        recvbuf[num] = '\0';
        printf("Received client(%s) message: %s", cli_name, recvbuf);

        // 处理客户端发送的数据
        int i = 0;
        for(i = 0;i < num - 1;i++){
            if((recvbuf[i] >= 'a' && recvbuf[i] <= 'z') || (recvbuf[i] >= 'A' && recvbuf[i] <= 'Z')){
                recvbuf[i] += 3;
                if((recvbuf[i] > 'Z' && recvbuf[i] < 'Z' + 3) || (recvbuf[i] > 'z')){
                    recvbuf[i] -= 26;
                }
                
            }
            sendbuf[i] = recvbuf[i];
        }
        // 将处理后的数据发送给客户端
        sendbuf[num - 1] = '\0';
        send(connfd, sendbuf, strlen(sendbuf), 0);
    }

    close(connfd);
    
}